﻿using Cassandra;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;

namespace dreamwork.cassandra.test.api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<int>> Get()
        {
            var cluster = Cluster.Builder()
                    .AddContactPoints("10.0.75.1")
                    .Build();

            // Connect to the nodes using a keyspace
            var session = cluster.Connect("demodb");
            // Execute a query on a connection synchronously
            //session.Execute("CREATE KEYSPACE demodb WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 3 };");
            //session.Execute("USE demodb;");
            //session.Execute(@"CREATE TABLE emp (
            //              empID int,
            //              deptID int,
            //              first_name varchar,
            //              last_name varchar,
            //              PRIMARY KEY(empID, deptID)); ");
            //session.Execute("INSERT INTO emp(empID,deptID) values (1,1);");
            var rs = session.Execute("SELECT * FROM emp");
            IList<int> values = new List<int>();
            // Iterate through the RowSet
            foreach (var row in rs)
            {
                var value = row.GetValue<int>("empid");
                // Do something with the value
                values.Add(value);
            }
            return values.ToList();
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public ActionResult<string> Get(int id)
        {
            return "value";
        }

        // POST api/values
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
